import request from '@/utils/request/req';

// 接口返回的通用响应格式
interface ApiResponse<T> {
  code: number;
  msg: string;
  data: T;
}

// 微信通知信息
interface WechatNotification {
  id: number;
  wx_id: string;
  send_wxid: string;
  state: number;
  sendWxList: Array<{
    user_name: string;
    user_avatar_url: string;
    wx_account: string;
    wechat_account_id: string;
    send_wxid: string;
    state: number;
    id: number;
    wx_id: string;
  }>;
  user_name: string;
  user_avatar_url: string;
  my_wx_id: string;
  user_open_id: string;
  user_open_id_name: string;
  user_create_time: string;
  user_info_update_time: string;
  wechat_account_id: string;
}

// 微信通知日志
interface WechatNotifyLog {
  id: number;
  wx_id: string;
  send_wx_id: string;
  message: string;
  date_time: string;
  aiUserName: string;
  aiUserAvatarUrl: string;
  sendUserName: string;
  sendUserAvatarUrl: string;
  status: string;
}

// 分页响应数据
interface PageResponse<T> {
  records: T[];
  total: number;
  size: number;
  current: number;
  pages: number;
}

/**
 * 查询微信通知
 * @param account 账号/微信ID
 * @param params 查询参数
 * @returns 返回微信通知信息
 */
export function getWechatNotification(
  account: string,
  params?: {
    operator?: string;
    notifyWechat?: string;
    current?: number;
    size?: number;
  }
): Promise<ApiResponse<PageResponse<WechatNotification>>> {
  return request({
    url: '/msg/seleteWechatNotification',
    method: 'get',
    params: { account, ...params }
  });
}

/**
 * 查询微信通知可引用的通知微信号列表
 * @param account 账号/微信ID
 * @returns 返回可用的通知微信号列表
 */
export function getWechatNotifyList(account: string): Promise<ApiResponse<WechatNotification[]>> {
  return request({
    url: '/msg/seleteWechatNotifyList',
    method: 'get',
    params: { account }
  });
}

/**
 * 保存微信通知
 * @param data 通知信息
 * @param data.id 通知ID（更新时必填）
 * @param data.wx_id AI微信ID
 * @param data.send_wxid_list 通知微信号列表
 * @param data.state 状态（0启用，1停用）
 * @param data.user_id 用户ID
 * @returns 返回操作结果
 */
export function updateWechatNotify(data: {
  send_wxid_list: Array<{
    id: number;
    wx_id: string;
    send_wxid: string;
    state: number;
  }>;
}): Promise<ApiResponse<null>> {
  return request({
    url: '/msg/updateWechatNotify',
    method: 'post',
    data
  });
}

/**
 * 查询微信介入通知日志
 * @param account 账号/微信ID
 * @param status 是否已读（0未读 1已读）
 * @param params 其他查询参数（如分页、筛选）
 * @returns 返回通知日志分页列表
 */
export function getWechatNotifyLog(
  account: string,
  status?: string,
  params?: {
    operator?: string;
    notifyWechat?: string;
    current?: number;
    size?: number;
  }
): Promise<ApiResponse<PageResponse<WechatNotifyLog>>> {
  return request({
    url: '/msg/seleteWechatNotifyLog',
    method: 'get',
    params: { account, status, ...params }
  });
}
